package chai.ningning.lifecycle;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

/*
	<servlet>
	<servlet-name>prepared</servlet-name>
	<servlet-class>com.itlaobing.lifecycle.PreparedServlet</servlet-class>
	
	<!-- 使用 load-on-startup 元素指定 容器 加载(load) 当前 Web应用 时是否对该Servlet进行初始化 -->
	<!-- 非负整数表示 初始化 的 顺序，数字越小越优先初始化 (但建议使用 正整数 )-->
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>prepared</servlet-name>
	<url-pattern>/prepared/*</url-pattern>
</servlet-mapping>
*/
@WebServlet( name = "prepared" , urlPatterns = "/prepared/*" , loadOnStartup = 1 )
public class PrepareServlet implements Servlet{
	static {
		System.out.println("一、类加载阶段："+ PassiveServlet.class.getCanonicalName());
	}

	private ServletConfig config ;
	
	public PrepareServlet() {
		super();
		System.out.println("二、Servlet 实例：" +this+ "创建完毕");
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("1、初始化 Servlet 实例："+ this );
		this.config = config;
	}

	@Override
	public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
	
		System.out.println("2、Servlet 实例 : "+ this +"正在提供服务");
		
		PrintWriter out = resp.getWriter();
		out.println("<div style='width:450px ;margin:15px auto ;text-align:center ; font-size:30px ; border:1px solid skyblue ;color:green '>");
		out.println(LocalDateTime.now());
		out.println("</div>");
	}
	
	@Override
	public void destroy() {
		System.out.println("3、释放Servlet实例 ：" +this+ "所占用的资源， 为回收该对象做准备");
	}

	@Override
	public ServletConfig getServletConfig() {
		// 返回实例变量config中所保存的引用(地址)
		return config;
	}

	@Override
	public String getServletInfo() {
		return " ";
	}
}
